package com.younion.action.manage;

import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.common.collect.Maps;
import com.younion.business.ICardEntityService;
import com.younion.business.IMsgTemplateService;
import com.younion.business.IServiceInitService;
import com.younion.common.base.BaseAction;
import com.younion.common.constants.Status;
import com.younion.common.constants.TemplateScope;
import com.younion.common.constants.TemplateTargetType;
import com.younion.common.paginator.Paginator;
import com.younion.vo.CardEntityVO;
import com.younion.vo.MemberCardVO;
import com.younion.vo.MembercardServiceVO;
import com.younion.vo.MsgTemplateVO;
import com.younion.vo.ServiceInitVO;

import cpcns.jhsks.util.MapCollectionUtils;

/**
 * @author wells
*/
public class ServiceInitAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	protected static final Log logger = LogFactory.getLog(ServiceInitAction.class);

	private IServiceInitService serviceInitService = null;

	private ServiceInitVO serviceInitVO = null;

	private List<ServiceInitVO> serviceInitList;// 结果集列表
	
	private List<MsgTemplateVO> serviceTemplates = null;
	
	@Autowired
	private ICardEntityService cardEntityService = null;
	
	@Autowired
	private IMsgTemplateService msgTemplateService = null;
	
	private List<TemplateScope> msgTypes = Arrays.asList(TemplateScope.values());
	
	/**
	 * 列表：翻页区；数据区
	 */
	@SuppressWarnings("unchecked")
	public String listServiceInit() {
		try {
			Map paramMap = null;
			if (serviceInitVO != null) {
				paramMap = MapCollectionUtils.removeEmptyEntry(PropertyUtils.describe(serviceInitVO));
			}
			int total = serviceInitService.getCountByMap(paramMap);
			Paginator paginator = this.getPaginator(total);
			serviceInitList = serviceInitService.get(paramMap, paginator);
			this.savaPaginator(paginator);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;
	}

	/**
	 * 新增
	 */
	public String addServiceInit() {
		try {
			serviceInitVO.setCardEntity(cardEntityService.getById(serviceInitVO.getCardEntity().getId().toString()));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;
	}

	/**
	 * 修改保存，调度JSP
	 */
	public String editServiceInit() {
		try {
			serviceInitVO = serviceInitService.getById(serviceInitVO.getId().toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	/**
	 * 保存
	 */
	public String saveServiceInit() {
		try {
			/*String permission = request.getParameter("permission");
			if(permission != null)
				serviceInitVO.setServiceId(permission.substring(0, permission.length()-1));*/
			if(serviceInitVO.getId() == null){
				//serviceInitVO.setMsgType("");
				MsgTemplateVO template =  msgTemplateService.getById(serviceInitVO.getTemplate().getId().toString());
				String name = "";
				if(!TemplateTargetType.COMMON.equals(template.getTargetType())){
					ServiceInitVO param = new ServiceInitVO();
					serviceInitVO.setSendTime(null);
					param.setCardEntity(new CardEntityVO(serviceInitVO.getCardEntity().getId()));
					Map paramMap = MapCollectionUtils.removeEmptyEntry(PropertyUtils.describe(serviceInitVO));
					Collection services = serviceInitService.getByMap(paramMap);
					for(Iterator<ServiceInitVO> iterator = services.iterator(); iterator.hasNext();){
						ServiceInitVO service = (ServiceInitVO) iterator.next(); 
						if(service.getTemplate().getTargetType().equals(template.getTargetType())){
							this.tipMessage = "该会员已经添加过" + service.getTemplate().getTargetTypeTitle();
							return ERROR;
						}
					}
					name = template.getTargetType();
				}else {
					name = template.getName();
				}
				
				this.addLog(3, "新增实体卡计划服务：【"+name == null ? "" : name+
						"】", "新增实体卡计划服务",getSessionUser());
			}else{
				this.addLog(3, "修改实体卡计划服务：【"+serviceInitVO.getId()+
						"】", "修改实体卡计划服务",getSessionUser());
			}
			serviceInitVO = serviceInitService.save(serviceInitVO);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		request.setAttribute("relDivId", "serviceInitListTab");
		return SUCCESS;
	}

	/**
	 * 删除
	 */
	public String deleteServiceInit() {
		try {
			String pkId = request.getParameter("key");
			serviceInitService.remove(Arrays.asList(pkId.split(",")));
			this.addLog(4, "实体卡服务计划："+pkId, "实体卡服务计划",getSessionUser());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		request.setAttribute("relDivId", "serviceInitListTab");
		return SUCCESS;
	}
	/**
	 * 查看鿴
	 */
	public String viewServiceInit() {
		try {
			String pkId = request.getParameter("key"); 
			serviceInitVO = serviceInitService.getById(pkId);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		return SUCCESS;

	}
	
	public List<MsgTemplateVO> getServiceTemplates() {
		if(serviceTemplates == null){
			try{
				Map loadMsgtemplateParamMap = Maps.newHashMap();
				loadMsgtemplateParamMap.put("status", Status.ACTIVE.getName());
				//loadMsgtemplateParamMap.put("targetType1", TemplateTargetType.BIRTHDAY.getName());
				//loadMsgtemplateParamMap.put("targetType2", TemplateTargetType.NOCOST.getName());
				//loadMsgtemplateParamMap.put("targetType3", TemplateTargetType.NORECHARGE.getName());
				loadMsgtemplateParamMap.put("notCommon", TemplateTargetType.COMMON.getName());
				
				if (getSessionUser().getCustomer() != null) {
					loadMsgtemplateParamMap.put("customerId", getSessionUser().getCustomer().getId());
				}
				serviceTemplates = (List<MsgTemplateVO>) msgTemplateService
						.getByMap(loadMsgtemplateParamMap);
			}catch(Exception e){
				
			}
		}
		return serviceTemplates;
	}

	public ServiceInitVO getServiceInitVO() {
		return serviceInitVO;
	}

	public void setServiceInitVO(ServiceInitVO serviceInitVO) {
		this.serviceInitVO = serviceInitVO;
	}

	public void setServiceInitService(IServiceInitService serviceInitService) {
		this.serviceInitService = serviceInitService;
	}

	public List<ServiceInitVO> getServiceInitList() {
		return serviceInitList;
	}

	public void setServiceInitList(List<ServiceInitVO> serviceInitList) {
		this.serviceInitList = serviceInitList;
	}

	public ICardEntityService getCardEntityService() {
		return cardEntityService;
	}

	public void setCardEntityService(ICardEntityService cardEntityService) {
		this.cardEntityService = cardEntityService;
	}

	public IMsgTemplateService getMsgTemplateService() {
		return msgTemplateService;
	}

	public void setMsgTemplateService(IMsgTemplateService msgTemplateService) {
		this.msgTemplateService = msgTemplateService;
	}

	public List<TemplateScope> getMsgTypes() {
		return msgTypes;
	}

	public void setMsgTypes(List<TemplateScope> msgTypes) {
		this.msgTypes = msgTypes;
	}

	public IServiceInitService getServiceInitService() {
		return serviceInitService;
	}

	public void setServiceTemplates(List<MsgTemplateVO> serviceTemplates) {
		this.serviceTemplates = serviceTemplates;
	}

}